
package com.ojoin.trade.supplyloan_core.web.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.supplyloan_core.web.domain.TbPlatformExpireVo;

import org.jasypt.commons.CommonUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.exception.DaoException;
import com.ojoin.trade.common.web.dao.impl.BaseDaoImpl;
import com.ojoin.trade.supplyloan_core.web.dao.TbSlPlatformUserDao;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformUser;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformUserVO;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformUserWithdrawVO;

/**
 * Created with IntelliJ IDEA. User: Administrator Date: 15-10-22 Time: 上午11:45
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class TbSlPlatformUserDaoImpl extends BaseDaoImpl<TbSlPlatformUser>
		implements TbSlPlatformUserDao {
	@Override
	/**
	 * 根据九盈用户id
	 * 批量新增电商平台用户
	 * @param list
	 * @throws DaoException
	 */
	public void insertBatchTbSlPlatformUser(List<TbSlPlatformUser> list)
			throws BusinessException {
		try {
			this.insertInBatch(list);
		} catch (Exception e) {
			throw new DaoException("批量插入电商平台用户表数据出错!语句：%s", e);
		}

	}
	
	public int deleteByJoinId(int joinId) throws BusinessException {
		String sqlName="deleteByJoinId";
		try {
			return sqlSessionTemplate.delete(getSqlName(sqlName), joinId);
		} catch (Exception e) {
			throw new DaoException(String.format("根据ID删除对象出错！语句：%s", getSqlName(sqlName)), e);
		}
		
	}

	@Override
	public List<TbSlPlatformUser> queryTbSlPlatFormByPlatformType(
			String platformName) throws BusinessException {
		String mapMethodName = "selectByType";
		try {
			List list = this.sqlSessionTemplate.selectList(
					getSqlName(mapMethodName), platformName);
			if (list != null && list.size() > 0)
				return list;
		} catch (Exception e) {
			throw new DaoException(String.format("查询某种类型平台的电商平台用户表出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
		return null;
	}

	@Override
	public List<TbSlPlatformUser> queryTbSlPlatFormByJoinidandName(
			String platformName, int idOfTbMerchantUser)
			throws BusinessException {
		String mapMethodName = "selectByJoinIDandType";
		Map<String, Object> paramap = new HashMap<String, Object>();
		paramap.put("platformName", platformName);
		paramap.put("idOfTbMerchantUser", idOfTbMerchantUser);
		List list = null;
		try {
			list = this.sqlSessionTemplate.selectList(
					getSqlName(mapMethodName), paramap);
			if (list != null && list.size() > 0)
				return list;
		} catch (Exception e) {
			throw new DaoException(
					String.format("查询某种类型平台,某位用户的电商平台用户表出错！语句：%s",
							getSqlName(mapMethodName)), e);
		}
		return list;

	}

	@Override
	public List<TbSlPlatformUser> selectPlatformUserListByJoinID(
			int idOfTbMerchantUser) throws BusinessException {
		String mapMethodName = "selectPlatformUserListByJoinID";
		List list = null;
		try {
			list = this.sqlSessionTemplate.selectList(
					getSqlName(mapMethodName), idOfTbMerchantUser);
			if (list != null && list.size() > 0)
				return list;
		} catch (Exception e) {
			throw new DaoException(String.format("查询用户" + idOfTbMerchantUser + "的电商平台用户列表出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
		return list;
	}

	@Override
	public List<TbSlPlatformUserVO> selectAllPlatformUserListByJoinID(
			int idOfTbMerchantUser, Pageable page) throws BusinessException {
		String mapMethodName = "selectAllPlatformUserListByJoinID";
		List list = null;
		try {
			list = this.sqlSessionTemplate.selectList(
					getSqlName(mapMethodName), idOfTbMerchantUser, this.getPageBounds(page));
			if (list != null && list.size() > 0)
				return list;
		} catch (Exception e) {
			throw new DaoException(String.format("查询用户" + idOfTbMerchantUser + "的需抓取数据电商平台用户列表出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
		return list;
	}
	
    @Override
    public void deleteByJoinandId(int joinid, int id) throws BusinessException {
        String sqlName="deleteByJoinAndId";
        Map<String,Object>  map=new HashMap<>();
        map.put("idOfTbMerchantUser",joinid);
        map.put("id",id);
        try {
             sqlSessionTemplate.delete(getSqlName(sqlName), map);
        } catch (Exception e) {
            throw new DaoException(String.format("根据ID和Joinid删除对象出错！语句：%s", getSqlName(sqlName)), e);
        }
    }

    @Override
	public void insertTbSlPlatformUser(TbSlPlatformUser uservo)
			throws BusinessException {
		try {
			this.insert(uservo);
		} catch (Exception e) {
			throw new DaoException("插入电商平台用户表数据出错!语句：%s", e);
		}
	}

	@Override
	public void updateTbSlPlatFormUser(Map<String, Object> map)
			throws BusinessException {
		String mapMethodName = "update";
		try {
			this.sqlSessionTemplate.update(getSqlName(mapMethodName), map);
		} catch (Exception e) {
			throw new DaoException(String.format("更新电商平台用户表数据出错!语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public List<TbSlPlatformUser> queryVagueByName(String platformName)
			throws BusinessException {

		String mapMethodName = "selectVagueByName";
		List<TbSlPlatformUser> list = null;
		try {
			list = this.sqlSessionTemplate.selectList(
					getSqlName(mapMethodName), platformName);
			if (list != null && list.size() > 0)
				return list;
		} catch (Exception e) {
			throw new DaoException(String.format("查询某种类型平台的电商平台用户表出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
		return null;
	}

	@Override
	public int updatePlatformUserEnableflagById(int id, String enableflag)
			throws BusinessException {
		String mapMethodName = "updatePlatformUserEnableflagById";
		try {
			HashMap<String, Object> parammap = new HashMap<String, Object>();
			parammap.put("id", id);
			parammap.put("enableFlag", enableflag);
			return this.sqlSessionTemplate.update(getSqlName(mapMethodName), parammap);
		} catch (Exception e) {
			throw new DaoException(String.format("更新电商平台用户的启用状态出错!语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public int updatePlatformUserEnableflagByPIdAndJId(int id, int joinuserid, String enableflag)
			throws BusinessException {
		String mapMethodName = "updatePlatformUserEnableflagByPIdAndJId";
		try {
			HashMap<String, Object> parammap = new HashMap<String, Object>();
			parammap.put("id", id);
			parammap.put("joinuserid", joinuserid);
			parammap.put("enableFlag", enableflag);
			return this.sqlSessionTemplate.update(getSqlName(mapMethodName), parammap);
		} catch (Exception e) {
			throw new DaoException(String.format("更新电商平台用户的启用状态出错!语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}
	
	@Override
	public int updatePlatformUserEnableflagByJoinuserId(int joinuserid,
			String enableFlag) throws BusinessException {
		String mapMethodName = "updatePlatformUserEnableflagByJoinuserId";
		try {
			HashMap<String, Object> parammap = new HashMap<String, Object>();
			parammap.put("joinuserid", joinuserid);
			parammap.put("enableFlag", enableFlag);
			return this.sqlSessionTemplate.update(getSqlName(mapMethodName), parammap);
		} catch (Exception e) {
			throw new DaoException(String.format("更新九盈用户的所有电商平台用户的启用状态出错!语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public int isExistPausedPlatformUserByJoinuserId(int joinuserid)
			throws BusinessException {
		String mapMethodName = "isExistPausedPlatformUserByJoinuserId";
		try {
			return this.sqlSessionTemplate.selectOne(getSqlName(mapMethodName), joinuserid);
		} catch (Exception e) {
			throw new DaoException(String.format("查询指定九盈用户是否存在状态为申请暂停的电商平台用户出错!语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}
 
	@Override
	public List<TbSlPlatformUserVO> selectPlatformUserListByNameAndFlag(
			String joinuserid, String platform_name, String enable_flag)
			throws BusinessException {
		String mapMethodName = "selectPlatformUserListByNameAndFlag";
		try {
			HashMap<String, Object> parammap = new HashMap<String, Object>();
			parammap.put("idOfTbMerchantUser", joinuserid);
			parammap.put("platform_name", platform_name);
			parammap.put("enableFlag", enable_flag);
			return this.sqlSessionTemplate.selectList(getSqlName(mapMethodName), parammap);
		} catch (Exception e) {
			throw new DaoException(String.format("根据九盈用户、电商平台名、启用状态查询电商平台用户列表出错!语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public List<TbSlPlatformUserWithdrawVO> selectPlatformUserWithdrawListByJoinuserId(
			int joinuserid) throws BusinessException {
		String mapMethodName = "selectPlatformUserWithdrawListByJoinuserId";
		try {
			HashMap<String, Object> parammap = new HashMap<String, Object>();
			parammap.put("joinuserid", joinuserid);
			return this.sqlSessionTemplate.selectList(getSqlName(mapMethodName), parammap);
		} catch (Exception e) {
			throw new DaoException(String.format("根据九盈用户查询其在各电商平台的可提限额详细列表出错!语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public int slPlatformUserCredit(TbSlPlatformUser uservo)
			throws BusinessException {
		String mapMethodName = "updateByPrimaryAndJoinid";
		try {
			return this.sqlSessionTemplate.update(getSqlName(mapMethodName), uservo);
		} catch (Exception e) {
			throw new DaoException(String.format("为九盈用户的电商平台分别授信!语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public int slPlatformUserCredit(TbSlPlatformUserVO uservo)
			throws BusinessException {
		String mapMethodName = "updateByPrimaryAndJoinid";
		TbSlPlatformUser new_uservo = new TbSlPlatformUser();
		new_uservo.setId(uservo.getId());
		new_uservo.setPlatformRuleId(uservo.getPlatformRuleId());
		new_uservo.setIdOfTbMerchantUser(uservo.getIdOfTbMerchantUser());
		new_uservo.setCreditCertificate(uservo.getCreditCertificate());
		new_uservo.setDiscount(uservo.getDiscount());
		new_uservo.setEnableFlag(uservo.getEnableFlag());
		new_uservo.setRate(uservo.getRate());
		new_uservo.setFee(uservo.getFee());
		new_uservo.setMaxAmount(uservo.getMaxAmount());
		new_uservo.setLoanPeriod(uservo.getLoanPeriod());
		new_uservo.setContractExpireDate(uservo.getContractExpireDate());
		
		try {
			return this.sqlSessionTemplate.update(getSqlName(mapMethodName), new_uservo);
		} catch (Exception e) {
			throw new DaoException(String.format("为九盈用户的电商平台分别授信!语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}
	
	@Override
	public int selectUncreditPlatformUserByJoinuserId(int joinuserid)
			throws BusinessException {
		String mapMethodName = "selectUncreditPlatformUserByJoinuserId";
        try{
            return this.sqlSessionTemplate.selectOne(getSqlName(mapMethodName),joinuserid);
        }catch (Exception e) {
            throw new DaoException(String.format("根据九盈用户查询未授信的电商平台个数出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
	}
	
	@Override
	public List<TbSlPlatformUserVO> selectPlatformUserListByNameAndFlag(
			String joinuserid, String platform_name, String enable_flag,Pageable page)
			throws BusinessException {
		String mapMethodName = "selectPlatformUserListByNameAndFlag";
		try {
			HashMap<String, Object> parammap = new HashMap<String, Object>();
			parammap.put("idOfTbMerchantUser", joinuserid);
			parammap.put("platformName", platform_name);
			parammap.put("enableFlag", enable_flag);
			return this.sqlSessionTemplate.selectList(getSqlName(mapMethodName), parammap,this.getPageBounds(page));
		} catch (Exception e) {
			throw new DaoException(String.format("根据九盈用户、电商平台名、启用状态查询电商平台用户列表出错!语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public void updateTbSlPlatFormUserPwd(int joinuserid, int platformuserid,
			String newpwd) throws BusinessException {
		String mapMethodName = "updateTbSlPlatFormUserPwd";
		try {
			HashMap<String, Object> parammap = new HashMap<String, Object>();
			parammap.put("joinuserid", joinuserid);
			parammap.put("platformuserid", platformuserid);
			parammap.put("newpwd", newpwd);
			this.sqlSessionTemplate.update(getSqlName(mapMethodName), parammap);
		} catch (Exception e) {
			throw new DaoException(String.format("更新电商平台用户密码出错!语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public int updateMerchantUserCreditInfoByJoinuserId(int joinuserid,
			int creditinfoid) throws BusinessException {
		String mapMethodName = "updateMerchantUserCreditInfoByJoinuserId";
		try {
			HashMap<String, Object> parammap = new HashMap<String, Object>();
			parammap.put("joinuserid", joinuserid);
			parammap.put("creditinfoid", creditinfoid);
			return this.sqlSessionTemplate.update(getSqlName(mapMethodName), parammap);
		} catch (Exception e) {
			throw new DaoException(String.format("更新九盈用户的所有电商平台征信信息出错!语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

    @Override
    public List<TbPlatformExpireVo> queryTbPlatformExpire(int joinuserid) throws BusinessException {
        String mapMethodName = "selectwillexpireforoperator";
        try {
            HashMap<String, Object> parammap = new HashMap<String, Object>();
            parammap.put("idOfTbMerchantUser", joinuserid);
            parammap.put("curdate", TimeUtils.getCurDate());
            return this.sqlSessionTemplate.selectList(getSqlName(mapMethodName), parammap);
        } catch (Exception e) {
            throw new DaoException(String.format("根据九盈用户、当前日期、查询电商平台用户过期数据出错!语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }

}

